<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<HTML>
<HEAD>
<TITLE>80386 Programmer's Reference Manual -- Section 3.7</TITLE>
</HEAD>
<BODY STYLE="width:80ch">
<B>up:</B> <A HREF="c03.htm">
Chapter 3 -- Applications Instruction Set</A><BR>
<B>prev:</B> <A HREF="s03_06.htm">3.6  String and Character Translation Instructions</A><BR>
<B>next:</B> <A HREF="s03_08.htm">3.8  Flag Control Instructions</A>
<P>
<HR>
<P>
<H1>3.7  Instructions for Block-Structured Languages</H1>
The instructions in this section provide machine-language support for
functions normally found in high-level languages. These instructions include
<A HREF="ENTER.htm">ENTER</A> and <A HREF="LEAVE.htm">LEAVE</A>, which simplify the programming of procedures.
<P>
<A HREF="ENTER.htm">ENTER</A> (Enter Procedure) creates a stack frame that may be used to implement
the scope rules of block-structured high-level languages. A <A HREF="LEAVE.htm">LEAVE</A>
instruction at the end of a procedure complements an <A HREF="ENTER.htm">ENTER</A> at the beginning
of the procedure to simplify stack management and to control access to
variables for nested procedures.
<P>
The <A HREF="ENTER.htm">ENTER</A> instruction includes two parameters. The first parameter
specifies the number of bytes of dynamic storage to be allocated on the
stack for the routine being entered. The second parameter corresponds to the
lexical nesting level (0-31) of the routine. (Note that the lexical level
has no relationship to either the protection privilege levels or to the I/O
privilege level.)
<P>
The specified lexical level determines how many sets of stack frame
pointers the CPU copies into the new stack frame from the preceding frame.
This list of stack frame pointers is sometimes called the display. The first
word of the display is a pointer to the last stack frame. This pointer
enables a <A HREF="LEAVE.htm">LEAVE</A> instruction to reverse the action of the previous <A HREF="ENTER.htm">ENTER</A>
instruction by effectively discarding the last stack frame.
Example:
<PRE>
<A HREF="ENTER.htm">ENTER</A> 2048,3
</PRE>
Allocates 2048 bytes of dynamic storage on the stack and sets up pointers
to two previous stack frames in the stack frame that <A HREF="ENTER.htm">ENTER</A> creates for
this procedure.
After <A HREF="ENTER.htm">ENTER</A> creates the new display for a procedure, it allocates the
dynamic storage space for that procedure by decrementing ESP by the number
of bytes specified in the first parameter. This new value of ESP serves as a
starting point for all <A HREF="PUSH.htm">PUSH</A> and <A HREF="POP.htm">POP</A> operations within that procedure.
<P>
To enable a procedure to address its display, <A HREF="ENTER.htm">ENTER</A> leaves EBP pointing to
the beginning of the new stack frame. Data manipulation instructions that
specify EBP as a base register implicitly address locations within the stack
segment instead of the data segment.
<P>
The <A HREF="ENTER.htm">ENTER</A> instruction can be used in two ways: nested and non-nested. If
the lexical level is 0, the non-nested form is used. Since the second
operand is 0, <A HREF="ENTER.htm">ENTER</A> pushes EBP, copies ESP to EBP and then subtracts the
first operand from ESP. The nested form of <A HREF="ENTER.htm">ENTER</A> occurs when the second
parameter (lexical level) is not 0.
<P>

<A HREF="s03_07.htm#fig3-16">Figure 3-16</A>
  gives the formal definition of <A HREF="ENTER.htm">ENTER</A>.
<P>
The main procedure (with other procedures nested within) operates at the
highest lexical level, level 1. The first procedure it calls operates at the
next deeper lexical level, level 2. A level 2 procedure can access the
variables of the main program which are at fixed locations specified by the
compiler. In the case of level 1, <A HREF="ENTER.htm">ENTER</A> allocates only the requested
dynamic storage on the stack because there is no previous display to copy.
<P>
A program operating at a higher lexical level calling a program at a lower
lexical level requires that the called procedure should have access to the
variables of the calling program. <A HREF="ENTER.htm">ENTER</A> provides this access through a
display that provides addressability to the calling program's stack frame.
<P>
A procedure calling another procedure at the same lexical level implies
that they are parallel procedures and that the called procedure should not
have access to the variables of the calling procedure. In this case, <A HREF="ENTER.htm">ENTER</A>
copies only that portion of the display from the calling procedure which
refers to previously nested procedures operating at higher lexical levels.
The new stack frame does not include the pointer for addressing the calling
procedure's stack frame.
<P>
<A HREF="ENTER.htm">ENTER</A> treats a reentrant procedure as a procedure calling another procedure
at the same lexical level. In this case, each succeeding iteration of the
reentrant procedure can address only its own variables and the variables of
the calling procedures at higher lexical levels. A reentrant procedure can
always address its own variables; it does not require pointers to the stack
frames of previous iterations.
<P>
By copying only the stack frame pointers of procedures at higher lexical
levels, <A HREF="ENTER.htm">ENTER</A> makes sure that procedures access only those variables of
higher lexical levels, not those at parallel lexical levels (see 
<A HREF="s03_07.htm#fig3-17">Figure 3-17</A>
  ).
<A HREF="s03_07.htm#fig3-18">Figures 3-18</A>
  through 3-21 demonstrate the actions of the <A HREF="ENTER.htm">ENTER</A>
instruction if the modules shown in 
<A HREF="s03_07.htm#fig3-17">Figure 3-17</A>
  were to call one another in
alphabetic order.
<P>
Block-structured high-level languages can use the lexical levels defined by
<A HREF="ENTER.htm">ENTER</A> to control access to the variables of previously nested procedures.
Referring to 
<A HREF="s03_07.htm#fig3-17">Figure 3-17</A>
  for example, if PROCEDURE A calls PROCEDURE B
which, in turn, calls PROCEDURE C, then PROCEDURE C will have access to the
variables of MAIN and PROCEDURE A, but not PROCEDURE B because they operate
at the same lexical level. Following is the complete definition of access to
variables for 
<A HREF="s03_07.htm#fig3-17">Figure 3-17</A>
  .
<OL>
<LI> MAIN PROGRAM has variables at fixed locations.

<LI> PROCEDURE A can access only the fixed variables of MAIN.

<LI> PROCEDURE B can access only the variables of PROCEDURE A and MAIN.
PROCEDURE B cannot access the variables of PROCEDURE C or PROCEDURE D.

<LI> PROCEDURE C can access only the variables of PROCEDURE A and MAIN.
PROCEDURE C cannot access the variables of PROCEDURE B or PROCEDURE D.

<LI> PROCEDURE D can access the variables of PROCEDURE C, PROCEDURE A, and
MAIN. PROCEDURE D cannot access the variables of PROCEDURE B.
</OL>
<A HREF="ENTER.htm">ENTER</A> at the beginning of the MAIN PROGRAM creates dynamic storage space
for MAIN but copies no pointers. The first and only word in the display
points to itself because there is no previous value for <A HREF="LEAVE.htm">LEAVE</A> to return to
EBP. See 
<A HREF="s03_07.htm#fig3-18">Figure 3-18</A>
  .
<P>
After MAIN calls PROCEDURE A, <A HREF="ENTER.htm">ENTER</A> creates a new display for PROCEDURE A
with the first word pointing to the previous value of EBP (BPM for <A HREF="LEAVE.htm">LEAVE</A> to
return to the MAIN stack frame) and the second word pointing to the current
value of EBP. Procedure A can access variables in MAIN since MAIN is at
level 1. Therefore the base for the dynamic storage for MAIN is at [EBP-2].
All dynamic variables for MAIN are at a fixed offset from this value. See

<A HREF="s03_07.htm#fig3-19">Figure 3-19</A>
  .
<P>
After PROCEDURE A calls PROCEDURE B, <A HREF="ENTER.htm">ENTER</A> creates a new display for
PROCEDURE B with the first word pointing to the previous value of EBP, the
second word pointing to the value of EBP for MAIN, and the third word
pointing to the value of EBP for A and the last word pointing to the current
EBP. B can access variables in A and MAIN by fetching from the display the
base addresses of the respective dynamic storage areas. See 
<A HREF="s03_07.htm#fig3-20">Figure 3-20</A>
  .
After PROCEDURE B calls PROCEDURE C, <A HREF="ENTER.htm">ENTER</A> creates a new display for
PROCEDURE C with the first word pointing to the previous value of EBP, the
second word pointing to the value of EBP for MAIN, and the third word
pointing to the EBP value for A and the third word pointing to the current
value of EBP. Because PROCEDURE B and PROCEDURE C have the same lexical
level, PROCEDURE C is not allowed access to variables in B and therefore
does not receive a pointer to the beginning of PROCEDURE B's stack frame.
See 
<A HREF="s03_07.htm#fig3-21">Figure 3-21</A>
  .
<P>
<A HREF="LEAVE.htm">LEAVE</A> (Leave Procedure) reverses the action of the previous <A HREF="ENTER.htm">ENTER</A>
instruction. The <A HREF="LEAVE.htm">LEAVE</A> instruction does not include any operands. 
<A HREF="LEAVE.htm">LEAVE</A>
copies EBP to ESP to release all stack space allocated to the procedure by
the most recent <A HREF="ENTER.htm">ENTER</A> instruction. Then <A HREF="LEAVE.htm">LEAVE</A> pops the old value of EBP from
the stack. A subsequent <A HREF="RET.htm">RET</A> instruction can then remove any arguments that
were pushed on the stack by the calling program for use by the called
procedure.
<P>
<A NAME="fig3-16">
<PRE>Figure 3-16.  Formal Definition of the ENTER Instruction</PRE>
<P>
The formal definition of the ENTER instruction for all cases is given by the
following listing. LEVEL denotes the value of the second operand.
<P>
Push EBP
Set a temporary value FRAME_PTR := ESP
If LEVEL > 0 then
<PRE>
      Repeat (LEVEL-1) times:
          EBP :=EBP - 4
          Push the doubleword pointed to by EBP
      End repeat
      Push FRAME_PTR
End if
EBP := FRAME_PTR
ESP := ESP - first operand.
</PRE>
<P>
<HR>
<P>
<A NAME="fig3-17">
<PRE>Figure 3-17.  Variable Access in Nested Procedures</PRE>
<P>
<PRE>
      +----------------------------------------------------------------+
      |                MAIN PROCEDURE (LEXICAL LEVEL 1)                |
      |   +--------------------------------------------------------+   |
      |   |              PROCEDURE A (LEXICAL LEVEL 2)             |   |
      |   |  +--------------------------------------------------+  |   |
      |   |  |           PROCEDURE B (LEXICAL LEVEL 3)          |  |   |
      |   |  +--------------------------------------------------+  |   |
      |   |                                                        |   |
      |   |  +--------------------------------------------------+  |   |
      |   |  |           PROCEDURE C (LEXICAL LEVEL 3)          |  |   |
      |   |  |  +--------------------------------------------+  |  |   |
      |   |  |  |        PROCEDURE D (LEXICAL LEVEL 4)       |  |  |   |
      |   |  |  +--------------------------------------------+  |  |   |
      |   |  |                                                  |  |   |
      |   |  +--------------------------------------------------+  |   |
      |   |                                                        |   |
      |   +--------------------------------------------------------+   |
      |                                                                |
      +----------------------------------------------------------------+
</PRE>
<P>
<HR>
<P>
<A NAME="fig3-18">
<PRE>Figure 3-18.  Stack Frame for MAIN at Level 1</PRE>
<P>
<PRE>
                                     * 31          0 *
                D  O                 |               |
                I  F              +- |-------+-------|
                R                 |  |    OLD ESP    |
                E  E     DISPLAY -|  |-------+-------|<--EBP FOR
                C  X              |  |      EBPM     |
                T  P              |- |-------+-------|
                I  A              |  |               |
                O  N              |  |-------+-------|
                N  S     DYNAMIC -|  |               |
                   I     STORAGE  |  |-------+-------|
                 | O              |  |               |
                 | N              +- |-------+-------|<--ESP
                 |                   |               |
                 !                   *               *
</PRE>
<P>
<HR>
<P>
<A NAME="fig3-19">
<PRE>Figure 3-19.  Stack Frame for Procedure A</PRE>
<P>
<PRE>
                                     * 31          0 *
                D  O                 |               |
                I  F                 |-------+-------|
                R                    |    OLD ESP    |
                E  E                 |-------+-------|
                C  X                 |      EBPM     |
                T  P                 |-------+-------|
                I  A                 |               |
                O  N                 |-------+-------|
                N  S                 |               |
                   I                 |-------+-------|
                 | O                 |               |
                 | N              +- |-------+-------|
                 |                |  |      EBPM     |
                 !                |  |-------+-------|<--EBP FOR A
                         DISPLAY -|  |      EBPM     |
                                  |  |-------+-------|
                                  |  |      EBPA     |
                                  |- |-------+-------|
                                  |  |               |
                                  |  |-------+-------|
                         DYNAMIC -|  |               |
                         STORAGE  |  |-------+-------|
                                  |  |               |
                                  +- |-------+-------|<--ESP
                                     |               |
                                     *               *
</PRE>
<P>
<HR>
<P>
<A NAME="fig3-20">
<PRE>Figure 3-20.  Stack Frame for Procedure B at Level 3 Called from A</PRE>
<P>
<PRE>
                                     * 31          0 *
                D  O                 |               |
                I  F                 |-------+-------|
                R                    |    OLD ESP    |
                E  E                 |-------+-------|
                C  X                 |      EBPM     |
                T  P                 |-------+-------|
                I  A                 |               |
                O  N                 |-------+-------|
                N  S                 |               |
                   I                 |-------+-------|
                 | O                 |               |
                 | N                 |-------+-------|
                 |                   |      EBPM     |
                 !                   |-------+-------|
                                     |      EBPM     |
                                     |-------+-------|
                                     |      EBPA     |
                                     |-------+-------|
                                     |               |
                                     |-------+-------|
                                     |               |
                                     |-------+-------|
                                     |               |
                                  +- |-------+-------|
                                  |  |      EBPA     |
                                  |  |-------+-------|<--EBP
                                  |  |      EBPM     |
                         DISPLAY -|  |-------+-------|
                                  |  |      EBPA     |
                                  |  |-------+-------|
                                  |  |      EBPB     |
                                  |- |-------+-------|
                                  |  |               |
                                  |  |-------+-------|
                         DYNAMIC -|  |               |
                         STORAGE  |  |-------+-------|
                                  |  |               |
                                  +- |-------+-------|<--ESP
                                     |               |
                                     *               *
</PRE>
<P>
<HR>
<P>
<A NAME="fig3-21">
<PRE>Figure 3-21.  Stack Frame for Procedure C at Level 3 Called from B</PRE>
<P>
<PRE>
                                     * 31          0 *
                D  O                 |               |
                I  F                 |-------+-------|
                R                    |    OLD ESP    |
                E  E                 |-------+-------|
                C  X                 |      EBPM     |
                T  P                 |-------+-------|
                I  A                 |               |
                O  N                 |-------+-------|
                N  S                 |               |
                   I                 |-------+-------|
                 | O                 |               |
                 | N                 |-------+-------|
                 |                   |      EBPM     |
                 !                   |-------+-------|
                                     |      EBPM     |
                                     |-------+-------|
                                     |      EBPA     |
                                     |-------+-------|
                                     |               |
                                     |-------+-------|
                                     |               |
                                     |-------+-------|
                                     |               |
                                  +- |-------+-------|
                                  |  |      EBPA     |
                                  |  |-------+-------|<--EBP
                                  |  |      EBPM     |
                         DISPLAY -|  |-------+-------|
                                  |  |      EBPA     |
                                  |  |-------+-------|
                                  |  |      EBPB     |
                                  |- |-------+-------|
                                  |  |               |
                                  |  |-------+-------|
                         DYNAMIC -|  |               |
                         STORAGE  |  |-------+-------|
                                  |  |               |
                                  +- |-------+-------|<--ESP
                                     |               |
                                     *               *
</PRE>
<P>
<HR>
<P>
<B>up:</B> <A HREF="c03.htm">
Chapter 3 -- Applications Instruction Set</A><BR>
<B>prev:</B> <A HREF="s03_06.htm">3.6  String and Character Translation Instructions</A><BR>
<B>next:</B> <A HREF="s03_08.htm">3.8  Flag Control Instructions</A>
</BODY>
